@file:Suppress("HasPlatformType")

package com.example.base.tools

import android.app.Application
import com.danikula.videocache.HttpProxyCacheServer
import java.io.File

/**  视频缓存工具类  **/
object CacheTool {

    private var application: Application? = null

    /**  初始化 CacheTool 工具  **/
    fun init(application: Application) {
        CacheTool.application = application
    }

    private val server: HttpProxyCacheServer by lazy {
        requireNotNull(application) {
            "You must be init the CacheTool!"
        }

        HttpProxyCacheServer.Builder(application)
            .maxCacheFilesCount(20)
            .build()
    }

    /**  传入需要缓存的 URL 地址, 得到缓存后的 URL 地址  **/
    fun getProxyUrl(url: String): String {
        // 若是本地文件则不进行代理, 若为 http 网络文件则进行代理
        var proxyPath = url
        if (url.startsWith(File.separator).not())
            proxyPath = server.getProxyUrl(url)
        return proxyPath
    }

}